package url;

import java.net.MalformedURLException;
import java.net.URL;

//URL:统一资源占位符
//URI:统一资源
public class URLDemo01 {
    public static void main(String[] args) {
        //绝对路径构建
        try {
            URL url=new URL("https://www.baidu.com:80/index.html#aa?username=lqk");
            System.out.println("协议:"+url.getProtocol());//查看协议
            System.out.println("域名:"+url.getHost());//查看域名
            System.out.println("端口:"+url.getPort());//查看端口
            System.out.println("资源:"+url.getFile());//查看资源
            System.out.println("相对路径资源:"+url.getPath());//相对路径资源
            System.out.println("锚点:"+url.getRef());//查看锚点
            System.out.println("参数:"+url.getQuery());//上面url=new URL("https://www.baidu.com:80/index.html#aa?username=lqk");存在锚点返回null

            url=new URL("https://www.baidu.com:80/a/");
            url=new URL(url,"index.html#aa?username=lqk");//相对路径
            System.out.println(url.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
